package com.hm.service.api.image;

import com.hm.service.service.upload.UploadPictureService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.nio.file.Paths;


@RestController
@RequestMapping(value = "/IMAGE", produces = MediaType.IMAGE_JPEG_VALUE)
public class SZCG_002_000_000 {

    private static final Logger logger = LoggerFactory.getLogger(SZCG_002_000_000.class);

    //图片加载接口
    public SZCG_002_000_000() {

    }

    @GetMapping(value = "/facePic")
    public ResponseEntity<Resource> doGet(HttpServletRequest request) {
        String id = request.getParameter("id");
        System.out.println("---filename->" + id);

        String token = request.getParameter("token");
        System.out.println("---token->" + token);

        String filePath = "D:/opencv-test/face-pic/";
        Path imageStoragePath = Paths.get(filePath);

        try {
            Path imagePath = imageStoragePath.resolve(id); // 假设图片名为image.jpg
            Resource resource = new UrlResource(imagePath.toUri());
            if (resource.exists() || resource.isReadable()) {
                return ResponseEntity.ok().body(resource);
            } else {
                throw new RuntimeException("Could not read the image: " + imagePath);
            }
        } catch (Exception e) {
            throw new RuntimeException("Error loading image", e);
        }
    }


}
